using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class followcamera : MonoBehaviour
{
    Transform player;
    public float xMargin=300;
    public float yMargin=400;
    float SmoothX = 10;
    float SmoothY = 10;

    public float MinCamX = 0;
    public float MinCamY = 0;
    public float MaxCamX = 300;
    public float MaxCamY = 300;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void Awake()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
    }
    bool NeedMoveX()
    {
        return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
    }
    bool NeedMoveY()
    {
        return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
    }
    private void FixedUpdate()
    {
        TrackPlayer();
    }
    void TrackPlayer()
    {
        float CamNewX = transform.position.x;
        float CamNewY = transform.position.y;
        if (NeedMoveX())
        {
            CamNewX = Mathf.Lerp(transform.position.x, player.position.x, SmoothX * Time.deltaTime);

        }
        if (NeedMoveY())
        {
            CamNewY = Mathf.Lerp(transform.position.y, player.position.y, SmoothY * Time.deltaTime);

        }
        CamNewX = Mathf.Clamp(CamNewX, MinCamX, MaxCamX);
        CamNewY = Mathf.Clamp(CamNewY, MinCamY, MaxCamY);
        transform.position = new Vector3(CamNewX, CamNewY, transform.position.z);

    }
}
